package swb.api.com.repository;

import swb.api.com.repository.dto.SmsVerificationCode;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.time.LocalDateTime;
import java.util.Optional;

@Repository
public interface SmsVerificationCodeRepository extends JpaRepository<SmsVerificationCode, Long> {
    Optional<SmsVerificationCode> findByMobile(String mobile);

    @Modifying
    @Query("DELETE FROM SmsVerificationCode s WHERE s.mobile = ?1")
    void deleteByMobile(String mobile);

    @Modifying
    @Query("DELETE FROM SmsVerificationCode s WHERE s.expireTime < ?1")
    void deleteExpiredCodes(LocalDateTime currentTime);

    // 添加查询方法：查找指定时间范围内的记录
    @Query("SELECT s FROM SmsVerificationCode s WHERE s.mobile = ?1 AND s.lastSendTime >= ?2")
    Optional<SmsVerificationCode> findByMobileAndTimeRange(String mobile, LocalDateTime startTime);
}
